﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YinfangWeb.BLL.Enum;
using YinfangWeb.Model;

namespace YinfangWeb.DAL
{
    public partial class ProductParamDAO
    {
        #region 保存属性参数母板数据，不修改默认图标文件ID
        public int UpdateNotDefaultFileID(ProductParam productParam, IDbTransaction trans = null)
        {
            var sql = "UPDATE ProductParam SET ProductDataID = @ProductDataID, ParamDefault = @ParamDefault, OrderNum = @OrderNum, ParamName1 = @ParamName1, ParamValue1 = @ParamValue1, ParamName2 = @ParamName2, ParamValue2 = @ParamValue2, ParamName3 = @ParamName3, ParamValue3 = @ParamValue3, PriceMarket = @PriceMarket, PriceSell=@PriceSell, PriceNet = @PriceNet, PriceTax = @PriceTax, PricePost = @PricePost, PriceCost = @PriceCost, CoinDeduct = @CoinDeduct, Stock = @Stock, SellSum = @SellSum WHERE ProductParamID = @ProductParamID";
            var result = 0;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, productParam, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, productParam);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 获取指定商品的子产品或母板记录列表
        /// <summary>
        /// 获取指定商品的子产品或母板记录列表
        /// </summary>
        /// <returns>实体列表</returns>
        public List<ProductParam> GetListByProductDataID(int productDataId, EnumProductParam_ParamDefault type=EnumProductParam_ParamDefault.All)
        {
            var wc = string.Empty;
            if (type != EnumProductParam_ParamDefault.All)
            {
                wc = $" AND ParamDefault={(int)type}";
            }
            var sql = $"SELECT * FROM ProductParam WHERE ProductDataID=@productDataId {wc} ORDER BY OrderNum";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                var q = conn.Query<ProductParam>(sql,new { productDataId });
                return q.ToList();
            }
        }
        #endregion
        #region 删除非母板子产品记录根据商品ID
        /// <summary>
        /// 删除非母板子产品记录根据商品ID
        /// </summary>
        /// <param name="productParamID">主键</param>
        /// <param name="trans">事务</param>
        /// <returns>影响行数</returns>
        public int DeleteByProductDataID(int productDataID, IDbTransaction trans = null)
        {
            var sql = "DELETE ProductParam WHERE ParamDefault=0 AND ProductDataID = @ProductDataID";
            var result = 0;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, new { ProductDataID = productDataID }, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, new { ProductDataID = productDataID });
                conn.Close();
                return result;
            }
        }
        #endregion
    }
}
